export function slowJs(times = 1000) {
  let st = Date.now()
    let fmr = wx.getFileSystemManager()
    let filePath = wx.env.USER_DATA_PATH + "/test.txt"
    for (let i=0; i< times; i++) {
      let fd = fmr.openSync({
        filePath,
        flag: "w",
      })
      fmr.writeSync({
        fd,
        data: `${Date.now()}`,
      })
      fmr.closeSync({
        fd
      })
    }
    console.log(`cost ${(Date.now() - st) / 1000} second`)
}

export function execLongTime(cnt=9999999) {
  let start = +new Date()
  let current = start
  for (let i = 0; i < cnt; i++) {
      current = +new Date()
  }
  console.log('used time: ', current - start)
}

export function getCallStack(filterLib = true) {
  let result = parseStackStrings(new Error().stack)

  return result
}

export function parseStackStrings(stackStr, filterLib = true) {
  const stacks = stackStr.split('\n')
  const REG_EXP = /at\s+([\S]+)\s+\((\S+)\)/
  let result = stacks.map((stack) => {
    const result = stack.match(REG_EXP)
    if (result && result[1] && result[2]) {
      const fileString = result[2].replace(/^\s*/, '').replace(/http:\/\/127\.0\.0\.1:\d+\/(:?(:?appservice|wxacrawler\/\d+\/program\/\w+)?\/)?/, '').replace(/https:\/\/(lib\/WASubContext.js)/, '$1')
      const [file, line, column] = fileString.split(':')
      if (fileString.split(':').length === 3) {
        return {
          func: result[1].replace(/^Audit_(setTimeout|setInterval)_?.*$/, '$1'),
          file,
          line: +line,
          column: +column
        }
      }
    }
    return null
  }).filter(stack => !!stack)

  return result
}